package com.aida.evil.eda.Window;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aida.evil.aida.R;
import com.aida.evil.eda.Toast.CustomSuperCardToast;
import com.github.johnpersano.supertoasts.SuperCardToast;

/****************************************************
 * com.aida.evil.eda.Window.CustomDialog
 * 作者：白雨濃  evil
 * 时间：16-4-7 18:38
 * 邮箱：evilbai@foxmail.com
 * 页面用途：中部弹出窗口
 ****************************************************/
public class CustomDialog extends Dialog
{
    /*窗口*/
    private static CustomDialog dialog;
    /*设置是否触摸外部,窗口消失,默认为true,消失*/
    private static boolean canceledOnTouchOutside = true;
    /*设置是否点击返回键,窗口消失,默认为true,消失*/
    private static boolean backPressed = true;
    /*布局容器*/
    private static View view;

    public CustomDialog(Context context)
    {
        super(context);
    }

    public CustomDialog(Context context, int themeResId)
    {
        super(context, themeResId);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener)
    {
        super(context, cancelable, cancelListener);
    }


    public static class Builder
    {
        private Context context;
        private String title;
        private String message;
        private float textSize;
        private String positiveButtonText;
        private String negativeButtonText;
        private String neutralButtonText;
        private View contentView;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;
        private DialogInterface.OnClickListener neutralButtonClickListener;


        public CustomDialog create()
        {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            dialog = new CustomDialog(context, R.style.Dialog);

            view = inflater.inflate(R.layout.dialog, null);

            dialog.addContentView(view, new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

            /*设置触摸外部消失*/
            dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);


            /*设置标题*/
            ((TextView) view.findViewById(R.id.dialog_title)).setText(title);


            /*设置积极按钮*/
            Button positiveButton = ((Button) view.findViewById(R.id.dialog_positiveButton));
            if (positiveButtonText != null)
            {
                positiveButton.setText(positiveButtonText);
                if (positiveButtonClickListener != null)
                {
                    positiveButton.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View v)
                        {
                            //dialog.dismiss();
                            positiveButtonClickListener.onClick(dialog,
                                    DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            }
            else
            {
                positiveButton.setVisibility(View.GONE);
            }


            /*设置消极按钮*/
            Button negativeButton = ((Button) view.findViewById(R.id.dialog_negativeButton));
            if (negativeButtonText != null)
            {
                negativeButton.setText(negativeButtonText);
                if (negativeButtonClickListener != null)
                {
                    negativeButton.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View v)
                        {
                            dialog.dismiss();
                            negativeButtonClickListener.onClick(dialog,
                                    DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            }
            else
            {
                negativeButton.setVisibility(View.GONE);
                view.findViewById(R.id.dialog_negativeButton_space).setVisibility(View.GONE);
            }


            /*设置中立按钮*/
            Button neutralButton = ((Button) view.findViewById(R.id.dialog_neutralButton));
            if (neutralButtonText != null)
            {
                neutralButton.setText(neutralButtonText);

                if (neutralButtonClickListener != null)
                {
                    neutralButton.setOnClickListener(new View.OnClickListener()
                    {
                        public void onClick(View v)
                        {
                            //dialog.dismiss();
                            neutralButtonClickListener.onClick(dialog,
                                    DialogInterface.BUTTON_NEUTRAL);
                        }
                    });
                }
            }
            else
            {
                neutralButton.setVisibility(View.GONE);
                view.findViewById(R.id.dialog_neutralButton_space).setVisibility(View.GONE);
            }


            /*设置内容*/
            if (message != null)
            {
                TextView messageTextView = ((TextView) view.findViewById(R.id.dialog_message));
                messageTextView.setText(message);
                messageTextView.setTextSize(textSize);
            }
            else if (contentView != null)
            {
                LinearLayout linearLayout_message = ((LinearLayout) view.findViewById(R.id.dialog_linearLayout_message));
                linearLayout_message.removeAllViews();
                linearLayout_message.addView(contentView, new LayoutParams(LayoutParams.MATCH_PARENT
                        , LayoutParams.MATCH_PARENT));
            }

            dialog.setContentView(view);
            return dialog;
        }

        public Builder(Context context)
        {
            this.context = context;
        }

        public Builder setMessage(String message)
        {
            this.message = message;
            return this;
        }

        public Builder setMessage(int message)
        {
            this.message = (String) context.getText(message);
            return this;
        }

        public Builder setMessageTextSize(float textSize)
        {
            this.textSize = textSize;
            return this;
        }


        public Builder setTitle(int title)
        {
            this.title = (String) context.getText(title);
            return this;
        }


        public Builder setTitle(String title)
        {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v)
        {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonText,
                                         DialogInterface.OnClickListener listener)
        {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener)
        {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener)
        {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener)
        {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNeutralButton(int neutralButtonText,
                                        DialogInterface.OnClickListener listener)
        {
            this.neutralButtonText = (String) context
                    .getText(neutralButtonText);
            this.neutralButtonClickListener = listener;
            return this;
        }

        public Builder setNeutralButton(String neutralButtonText,
                                        DialogInterface.OnClickListener listener)
        {
            this.neutralButtonText = neutralButtonText;
            this.neutralButtonClickListener = listener;
            return this;
        }

        /**
         * Toast
         *
         * @param context
         * @return
         */
        public Snackbar showToast(String context)
        {
            Snackbar snackbar = Snackbar.make(view, context, Snackbar.LENGTH_SHORT);
            snackbar.setActionTextColor(view.getResources().getColor(R.color.pblue));
            return snackbar;
        }

        /**
         * 显示窗口
         *
         * @return
         */
        public CustomDialog show()
        {
            dialog = create();
            dialog.show();
            return dialog;
        }

        /**
         * 窗口消失
         */
        public void dismiss()
        {
            dialog.dismiss();
        }

        /**
         * 设置触摸外部窗口是否消失
         *
         * @param b
         */
        public void setCanceledOnTouchOutside(boolean b)
        {
            canceledOnTouchOutside = b;
        }

        public void setBackPressed(boolean b)
        {
            backPressed = b;
        }

    }

    @Override
    public void onBackPressed()
    {
        if (backPressed)
        {
            super.onBackPressed();
        }
    }
}
